#!/bin/bash
  if which tput >/dev/null 2>&1; then
      ncolors=$(tput colors)
  fi
  if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
    RED="$(tput setaf 1)"
    GREEN="$(tput setaf 2)"
    YELLOW="$(tput setaf 3)"
    BLUE="$(tput setaf 4)"
    BOLD="$(tput bold)"
    NORMAL="$(tput sgr0)"
  else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    BOLD=""
    NORMAL=""
  fi
port_dropbear=`ps aux | grep dropbear | awk NR==1 | awk '{print $17;}'`
#porta2=`ps aux | grep dropbear | awk '/-p/ {print $21;}'`

#echo "QUAL PORTA?"
#echo "   1) $porta1 "
#echo "   2) $porta2 "
#read -p "Selecione a opção: " port_dropbear

log=/var/log/auth.log
loginsukses='Password auth succeeded'
clear
printf "${BLUE}${BOLD}"
echo ' '
echo "==================================================="
printf "${YELLOW}${BOLD}"
echo "               Monitor de Clientes                 "
printf "${BLUE}${BOLD}"
echo "==================================================="
printf "${YELLOW}${BOLD}"
echo "   USER            |   PID     | Conectado desde:  "
printf "${BLUE}${BOLD}"
echo "==================================================="
pids=`ps ax |grep dropbear |grep  " $port_dropbear" |awk -F" " '{print $1}'`
for pid in $pids
do
    pidlogs=`grep $pid $log |grep "$loginsukses" |awk -F" " '{print $3}'`
    i=0
    for pidend in $pidlogs
    do
      let i=i+1
    done
    if [ $pidend ];then
       login=`grep $pid $log |grep "$pidend" |grep "$loginsukses"`
       PID=$pid
       user=`echo $login |awk -F" " '{print $10}' | sed -r "s/'/ /g"`
       waktu=`echo $login |awk -F" " '{print $2"-"$1,$3}'`
       while [ ${#waktu} -lt 13 ]
       do
           waktu=$waktu" "
       done

       while [ ${#user} -lt 16 ]
       do
           user=$user" "
       done
       while [ ${#PID} -lt 8 ]
       do
           PID=$PID" "
       done
		printf "${YELLOW}${BOLD}"
       echo "  $user |  $PID | $waktu  "
		printf "${BLUE}${BOLD}"
       echo "---------------------------------------------------"
    fi
done
totalu=( `ps aux | grep -i dropbear | awk '{print $2}' | wc -l`);
total=$(( $totalu - 1 ))
printf "${YELLOW}${BOLD}"
echo "   TOTAL: $total Clientes online"
echo ""
printf "${NORMAL}"
exit 0

#!/bin/bash
  if which tput >/dev/null 2>&1; then
      ncolors=$(tput colors)
  fi
  if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
    RED="$(tput setaf 1)"
    GREEN="$(tput setaf 2)"
    YELLOW="$(tput setaf 3)"
    BLUE="$(tput setaf 4)"
    BOLD="$(tput bold)"
    NORMAL="$(tput sgr0)"
  else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    BOLD=""
    NORMAL=""
  fi
port_dropbear=`ps aux | grep dropbear | awk NR==1 | awk '{print $17;}'`
#porta2=`ps aux | grep dropbear | awk '/-p/ {print $21;}'`

#echo "QUAL PORTA?"
#echo "   1) $porta1 "
#echo "   2) $porta2 "
#read -p "Selecione a opção: " port_dropbear

log=/var/log/auth.log
loginsukses='Password auth succeeded'
clear
printf "${BLUE}${BOLD}"
echo ' '
echo "==================================================="
printf "${YELLOW}${BOLD}"
echo "               Monitor de Clientes                 "
printf "${BLUE}${BOLD}"
echo "==================================================="
printf "${YELLOW}${BOLD}"
echo "   USER            |   PID     | Conectado desde:  "
printf "${BLUE}${BOLD}"
echo "==================================================="
pids=`ps ax |grep dropbear |grep  " $port_dropbear" |awk -F" " '{print $1}'`
for pid in $pids
do
    pidlogs=`grep $pid $log |grep "$loginsukses" |awk -F" " '{print $3}'`
    i=0
    for pidend in $pidlogs
    do
      let i=i+1
    done
    if [ $pidend ];then
       login=`grep $pid $log |grep "$pidend" |grep "$loginsukses"`
       PID=$pid
       user=`echo $login |awk -F" " '{print $10}' | sed -r "s/'/ /g"`
       waktu=`echo $login |awk -F" " '{print $2"-"$1,$3}'`
       while [ ${#waktu} -lt 13 ]
       do
           waktu=$waktu" "
       done

       while [ ${#user} -lt 16 ]
       do
           user=$user" "
       done
       while [ ${#PID} -lt 8 ]
       do
           PID=$PID" "
       done
		printf "${YELLOW}${BOLD}"
       echo "  $user |  $PID | $waktu  "
		printf "${BLUE}${BOLD}"
       echo "---------------------------------------------------"
    fi
done
totalu=( `ps aux | grep -i dropbear | awk '{print $2}' | wc -l`);
total=$(( $totalu - 1 ))
printf "${YELLOW}${BOLD}"
echo "   TOTAL: $total Clientes online"
echo ""
printf "${NORMAL}"
exit 0

